﻿using Dm;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model.Entity;
using Model.Other;
using Model.Vo;
using RCWebApplication.CfgFile;
using Service;
using Service.Util;
using SqlSugar;
using System.Collections;

namespace RCWebApplication.Controllers
{
    [Route("[controller]/[Action]")]
    [ApiController]
    [Authorize]
    public class userController : ControllerBase
    {

        private readonly ILogger<userController> _logger;
        private ITokenUtil TokenUtil;
        private UserService userService;
        private readonly ISqlSugarClient db;
        private readonly SymmetrySecret symmetrySecret;
        private LoginInfoService loginInfoService;
        private HostHolder hostHolder;
       
        public userController(ILogger<userController> logger, 
            ITokenUtil tokenUtil, 
            ISqlSugarClient db, 
            UserService userService,
            SymmetrySecret symmetrySecret, 
            LoginInfoService loginInfoService,
            HostHolder hostHolder)
        {
            this.db = db;
            _logger = logger;
            TokenUtil = tokenUtil;
            this.userService = userService;
            this.symmetrySecret = symmetrySecret;
            this.loginInfoService = loginInfoService;
            this.hostHolder = hostHolder;
        }



        [HttpGet]
        public string get()
        {
            _logger.LogInformation("this is a get request!");
            //Hashtable hashtable = new Hashtable();
            //hashtable.Add("userId", hostHolder.getUser().user_id);
            return "this is a get request!";
        }

        [HttpPost]
        [AllowAnonymous]
        public ApiResult login(LoginVo loginVo)
        {
            // 参数校验
            if (loginVo == null || string.IsNullOrEmpty(loginVo.account)) {
                _logger.LogError("参数验证失败！");
                return ResultHelper.failed("参数验证失败！");
            }
            // 检索用户
            SysUser sysUser = new SysUser();
            try {
                sysUser = userService.findSysUserByAccount(loginVo.account);
            }
            catch(Exception ex) {
                _logger.LogError(CommonConstants.MSG_E_LOG_002);
                return ResultHelper.failed(CommonConstants.MSG_E_LOG_002);
            }

            // 验证账号和密码
            if (sysUser == null) {
                _logger.LogError(CommonConstants.MSG_E_LOG_002);
                return ResultHelper.failed(CommonConstants.MSG_E_LOG_002);
            }

            if (!sysUser.password.Equals(symmetrySecret.Encrypt(loginVo.password)))
            {
                _logger.LogError(CommonConstants.MSG_E_LOG_001);
                return ResultHelper.failed(CommonConstants.MSG_E_LOG_001);
            }

            //获取用户的登录信息
            LoginInfo loginInfo =  loginInfoService.getLoginInfoByUserId(sysUser.user_id);
            // 更新用户的登录信息
            loginInfoService.updateLoginInfo(loginInfo, sysUser.user_id, true);

            // 生成TOKEN

            String token = TokenUtil.getToken(sysUser.user_id.ToString());
            Hashtable hashtable= new Hashtable();
            hashtable.Add("userId", sysUser.user_id);
            hashtable.Add("Authorization", token);

           

            return ResultHelper.success(hashtable, CommonConstants.MSG_S_LOG_001);
        }

    }
}
